﻿using System;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;

namespace CodeCounsel.SharePoint.RibbonDesigner.Model
{
    [XName("Group", Namespaces.CommandUIText)]
    public class Group
        : ChildCollectionDomainElement<ControlContainer, Control>, ISequential
    {
        public string Template
        {
            get { return DataModel.GetAttributeValue(Schema.Template); }
            set { DataModel.SetAttributeValue(Schema.Template, value);}
        }

        public string Title
        {
            get { return DataModel.GetAttributeValue(Schema.Title); }
            set { DataModel.SetAttributeValue(Schema.Title, value); }
        }

        public int Sequence
        {
            get { return DataModel.GetAttributeValue<int>(Schema.Sequence, 1000); }
            set { DataModel.SetAttributeValue<int>(Schema.Sequence, value); }
        }

        public Group(XElement dataModel, DomainModelSource source)
            : base(dataModel, source)
        {
        }

        public Group()
        {
            Children.Add(new ControlContainer());
        }

        protected override void RaiseDomainElementRemoved(DomainElement item)
        {
            if (item is ControlContainer)
            {
                OnControlContainerRemoved(new DomainElementEventArgs<ControlContainer>((ControlContainer)item));
            }
        }

        protected override void RaiseChildDomainElementRemoved(Control control)
        {
            OnControlRemoved(new DomainElementEventArgs<Control>(control));
        }

        protected virtual void OnControlRemoved(DomainElementEventArgs<Control> e)
        {
            if (ControlRemoved != null)
            {
                ControlRemoved(this, e);
            }
        }

        protected virtual void OnControlContainerRemoved(DomainElementEventArgs<ControlContainer> e)
        {
            if (ControlContainerRemoved != null)
            {
                ControlContainerRemoved(this, e);
            }
        }

        public event EventHandler<DomainElementEventArgs<ControlContainer>> ControlContainerRemoved;
        public event EventHandler<DomainElementEventArgs<Control>> ControlRemoved;
    }
}
